﻿using Furion.LinqBuilder;
using Furion.Localization;
using ST.Torch.WebApi.Application.Utils.Dtos;
using ST.Torch.WebApi.Application.Utils.Services;
using ST.Torch.WebApi.Core.Entities.Utils;
using System.Data;
using System.Linq.Expressions;
using MiniExcelLibs;
using ST.Torch.WebApi.Application.AOP;


namespace ST.Torch.WebApi.Application.Utils
{

    /// <summary>
    /// 系统服务接口
    /// </summary>
    [AppAuthorize]
    public class AutomaticCodeGenerationAppService : IDynamicApiController
	{
		private readonly IAutomaticCodeGenerationService _AutomaticCodeGenerationService;

        public AutomaticCodeGenerationAppService(IAutomaticCodeGenerationService AutomaticCodeGenerationservice)
        {
            _AutomaticCodeGenerationService = AutomaticCodeGenerationservice;
        }


        [HttpPost]
		public async Task<PagedList<AutomaticCodeGenerationListDto>> GetDataPagedList([FromForm] PageReq req)
		{
			Expression<Func<AutomaticCodeGeneration, bool>> predicate = u => true;
            predicate=predicate.AndIf(!string.IsNullOrEmpty(req.key),u=>u.InputStr.Contains(req.key));
			
			var dataList= await _AutomaticCodeGenerationService.QueryPageAsync(predicate, req);
			PagedList<AutomaticCodeGenerationListDto> resultList = dataList.Adapt<PagedList<AutomaticCodeGenerationListDto>>();
			return resultList;
		}
        [HttpPost]
        public async Task<AutomaticCodeGenerationListDto> Single([FromForm] Guid Id)
        {
           
           var entity= await _AutomaticCodeGenerationService.SingleAsync(u=>u.Id==Id);
            AutomaticCodeGenerationListDto resultData = entity.Adapt<AutomaticCodeGenerationListDto>();
			return resultData;

        }
        [HttpPost]
        [OperateAuth("GeneralAddDataPerm")]
        public async Task Insert([FromForm] AutomaticCodeGenerationDto model)
        {
            AutomaticCodeGeneration entity = model.Adapt<AutomaticCodeGeneration>();
            await _AutomaticCodeGenerationService.InsertAsync(entity);
        }
		[HttpPost]
        [OperateAuth("GeneralAddDataPerm-")]
        public async Task Update([FromForm] AutomaticCodeGenerationListDto model)
		{
			
			var entity=await _AutomaticCodeGenerationService.SingleAsync(u => u.Id == model.Id);
			
            entity.InputStr=model.InputStr;
            entity.User1ImgUrl=model.User1ImgUrl;
            entity.User2ImgUrl=model.User2ImgUrl;
            entity.User1FileUrl=model.User1FileUrl;
            entity.User2FileUrl=model.User2FileUrl;
            entity.Content1Html=model.Content1Html;
            entity.Content2Html=model.Content2Html;
            entity.SelectVal=model.SelectVal;
            entity.StartType=model.StartType;
            entity.NumberVal1=model.NumberVal1;
            entity.NumberVal2=model.NumberVal2;
            entity.IsShow1=model.IsShow1;
            entity.IsShow2=model.IsShow2;
            entity.XXTime=model.XXTime;
            await _AutomaticCodeGenerationService.UpdateAsync(entity);
		}
		[HttpPost]
		public async Task DeleteAsync([FromForm] Guid Id)
		{
			var entity = await _AutomaticCodeGenerationService.SingleAsync(u => u.Id == Id);
			await _AutomaticCodeGenerationService.DeleteAsync(entity,true);
		}

        [HttpPost]
        public async Task DeleteBatchAsync([FromForm] List<Guid> Ids)
        {
            var entitys = await _AutomaticCodeGenerationService.QueryAsync(u => Ids.Contains(u.Id));
            foreach (var entity in entitys)
            {
                await _AutomaticCodeGenerationService.DeleteAsync(entity, true);
            }
        }

        /// <summary>
        /// 导出记录Excel
        /// </summary>
        /// <returns></returns>
        [HttpGet, QueryParameters, NonUnify]
        [OperateAuth("GeneralExportDataPerm-")]
        public async Task<dynamic> ExPortExcel(string key)
        {
            Expression<Func<AutomaticCodeGeneration, bool>> predicate = u => true;
            predicate = predicate.AndIf(!string.IsNullOrEmpty(key), u => u.InputStr.Contains(key));
            var dataList= await _AutomaticCodeGenerationService.QueryAsync(predicate);
            var ExData = dataList.Adapt<List<AutomaticCodeGenerationDto>>();
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(ExData);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "AutomaticCodeGeneration"+DateTime.Now.ToString("HHmmss")+".xlsx"
            };

        }

     


       


    }

}
